package cn.kgm.makeGod;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 角色类
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Character {
    private int id;
    private String name;
    private int level;
    private int charm;
    private int luck;
    private int attack;
    private int defense;
    private int maxHp;
    private int currentHp;
    private int age;
    private RandomCharacterGenerator.Gender gender;
    private String appearanceDescription;
    private RandomCharacterGenerator.SexualOrientation sexualOrientation;
    private Location currentLocation;
    private Map<PersonalityTag, Integer> personalities = new HashMap<>();
    private Map<Integer, Relationship> relationships = new HashMap<>();
    private Map<Integer, Integer> initialItems = new HashMap<>(); // 初始物品

    public Character(int id, String name, int level, int charm, int luck) {
        this.id = id;
        this.name = name;
        this.level = level;
        this.charm = charm;
        this.luck = luck;
    }

    // ... 之前的代码 ...

    /**
     * 设置初始物品
     */
    public void setInitialItem(int itemId, int quantity) {
        initialItems.put(itemId, quantity);
    }

    /**
     * 获取初始物品
     */
    public Map<Integer, Integer> getInitialItems() {
        return initialItems;
    }

    // 添加getter和setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getCharm() {
        return charm;
    }

    public void setCharm(int charm) {
        this.charm = charm;
    }

    public int getLuck() {
        return luck;
    }

    public void setLuck(int luck) {
        if (luck < 0) {
            this.luck = 1;
            return;
        }
        this.luck = luck;
    }

    public int getAttack() {
        return attack;
    }

    public void setAttack(int attack) {
        this.attack = attack;
    }

    public int getDefense() {
        return defense;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public int getMaxHp() {
        return maxHp;
    }

    public void setMaxHp(int maxHp) {
        this.maxHp = maxHp;
    }

    public int getCurrentHp() {
        return currentHp;
    }

    public void setCurrentHp(int currentHp) {
        this.currentHp = currentHp;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public RandomCharacterGenerator.Gender getGender() {
        return gender;
    }

    public void setGender(RandomCharacterGenerator.Gender gender) {
        this.gender = gender;
    }

    public String getAppearanceDescription() {
        return appearanceDescription;
    }

    public void setAppearanceDescription(String appearanceDescription) {
        this.appearanceDescription = appearanceDescription;
    }

    public RandomCharacterGenerator.SexualOrientation getSexualOrientation() {
        return sexualOrientation;
    }

    public void setSexualOrientation(RandomCharacterGenerator.SexualOrientation sexualOrientation) {
        this.sexualOrientation = sexualOrientation;
    }

    public Location getCurrentLocation() {
        return currentLocation;
    }

    public void setCurrentLocation(Location currentLocation) {
        this.currentLocation = currentLocation;
    }

    public Map<PersonalityTag, Integer> getPersonalities() {
        return personalities;
    }

    public void addPersonality(PersonalityTag tag, int intensity) {
        personalities.put(tag, intensity);
    }

    public boolean hasPersonality(PersonalityTag tag, int minIntensity) {
        Integer intensity = personalities.get(tag);
        return intensity != null && intensity >= minIntensity;
    }

    public Map<Integer, Relationship> getRelationships() {
        return relationships;
    }

    public void setRelationships(Map<Integer, Relationship> relationships) {
        this.relationships = relationships;
    }

    public void addRelationship(Relationship relationship) {
        relationships.put(relationship.getTargetId(), relationship);
    }

    public Relationship getRelationshipTo(int targetId) {
        return relationships.get(targetId);
    }

    /**
     * 获取与指定角色的关系
     */
    public Relationship getRelationshipWith(int targetId) {
        return relationships.get(targetId);
    }

    /**
     * 获取所有朋友
     */
    public List<Integer> getFriends() {
        List<Integer> friends = new ArrayList<>();
        for (Relationship rel : relationships.values()) {
            if (rel.getType() == RelationshipType.FRIEND && rel.getValue() > 30) {
                friends.add(rel.getTargetId());
            }
        }
        return friends;
    }

    /**
     * 获取所有敌人
     */
    public List<Integer> getEnemies() {
        List<Integer> enemies = new ArrayList<>();
        for (Relationship rel : relationships.values()) {
            if (rel.getType() == RelationshipType.SWORN_ENEMY && rel.getValue() < -30) {
                enemies.add(rel.getTargetId());
            }
        }
        return enemies;
    }

    /**
     * 获取家庭成员
     */
    public List<Integer> getFamilyMembers() {
        List<Integer> family = new ArrayList<>();
        for (Relationship rel : relationships.values()) {
            if (rel.getType() == RelationshipType.FAMILY) {
                family.add(rel.getTargetId());
            }
        }
        return family;
    }

    /**
     * 获取角色关系摘要
     */
    public String getRelationshipSummary() {
        StringBuilder sb = new StringBuilder();
        sb.append("关系摘要:\n");

        List<Integer> friends = getFriends();
        if (!friends.isEmpty()) {
            sb.append("  朋友: ").append(friends.size()).append("人\n");
        }

        List<Integer> enemies = getEnemies();
        if (!enemies.isEmpty()) {
            sb.append("  敌人: ").append(enemies.size()).append("人\n");
        }

        List<Integer> family = getFamilyMembers();
        if (!family.isEmpty()) {
            sb.append("  家人: ").append(family.size()).append("人\n");
        }

        if (friends.isEmpty() && enemies.isEmpty() && family.isEmpty()) {
            sb.append("  暂无重要关系\n");
        }

        return sb.toString();
    }

    /**
     * 获取角色详细信息
     */
    public String getDetailedInfo() {
        StringBuilder sb = new StringBuilder();
        sb.append("ID: ").append(id).append("\n");
        sb.append("姓名: ").append(name).append("\n");
        sb.append("性别: ").append(gender.getDisplayName()).append("\n");
        sb.append("年龄: ").append(age).append("\n");
        sb.append("等级: ").append(level).append("\n");
        sb.append("魅力: ").append(charm).append("\n");
        sb.append("运气: ").append(luck).append("\n");
        sb.append("攻击: ").append(attack).append("\n");
        sb.append("防御: ").append(defense).append("\n");
        sb.append("生命: ").append(currentHp).append("/").append(maxHp).append("\n");
        sb.append("性取向: ").append(sexualOrientation.getDisplayName()).append("\n");
        sb.append("外貌: ").append(appearanceDescription).append("\n");

        sb.append("性格: ");
        if (personalities.isEmpty()) {
            sb.append("无");
        } else {
            for (Map.Entry<PersonalityTag, Integer> entry : personalities.entrySet()) {
                sb.append(entry.getKey().getDisplayName())
                        .append("(").append(entry.getValue()).append(") ");
            }
        }
        sb.append("\n");

        if (currentLocation != null) {
            sb.append("位置: ").append(currentLocation.getName()).append("\n");
        }

        return sb.toString();
    }
}

